/*  dreic -- a compiler for Drei
 *
 *  $Id: Report.scala 2976 2006-10-18 14:34:08Z mihaylov $
 */

package dreic


/** This class provides functionality for printing error messages.
 */
object Report {

  /** Number of errors.
   */
  var errors: Int = 0

  /** Prints out an error message.
   */
  def error(message: String): Unit = {
    errors = errors + 1
    System.err.println(message)
  }

  /** Prints out an error message.
   */
  def error(position: Int, message: String): Unit = {
    errors = errors + 1
    print(position, message)
  }

  /** Prints out an error message and stops the program execution.
   */
  def fail(message: String): Unit = {
    error(message)
    System.exit(1)
  }

  /** Prints out an error message and stops the program execution.
   */
  def fail(position: Int, message: String): Unit = {
    error(position, message)
    System.exit(1)
  }

  def exit(): Unit = {
    if (Report.errors > 0) {
      System.err.println()
      System.err.println(errors + " error" + (if (errors > 1) "s" else "") + " found." )
    }
    System.exit(if (errors > 0) 1 else 0)
  }

  /** Imprime la position et le message passés en argument. */
  private def print(pos: Int, message: String): Unit =
    System.err.println(Position.toString(pos) + ": " + message)

}
